﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace DotNetCommon.Logger
{
    /// <summary>
    /// 日志记录器工厂
    /// </summary>
    public class LoggerFactory
    {
        private static ConcurrentDictionary<string, ILogger> _loggerCache = new ConcurrentDictionary<string, ILogger>();

        /// <summary>
        /// 根据类别创建日志记录器
        /// </summary>
        /// <typeparam name="T">以此泛型全称作为类别名</typeparam>
        /// <returns></returns>
        public static ILogger<T> CreateLogger<T>()
        {
            return _loggerCache.GetOrAdd(typeof(T).FullName, (key) => new InternalLogger<T>(typeof(T).FullName)) as ILogger<T>;
        }

        /// <summary>
        /// 根据类别创建日志记录器
        /// </summary>
        /// <param name="categoryName">类别名</param>
        /// <returns></returns>
        public static ILogger CreateLogger(string categoryName)
        {
            return _loggerCache.GetOrAdd(categoryName, (key) => new InternalLogger<object>(categoryName));
        }

        internal static Action<LogContext> logAction = null;
        internal static bool enableDefaultOutPut = true;

        /// <summary>
        /// 设置DotNetCommon的日志输出
        /// </summary>
        /// <param name="logAction"></param>
        /// <param name="enableDefaultOutPut"></param>
        public static void SetLogger(Action<LogContext> logAction, bool enableDefaultOutPut)
        {
            LoggerFactory.logAction = logAction;
            LoggerFactory.enableDefaultOutPut = enableDefaultOutPut;
        }
    }
}
